package com.situ.mvc.controller;

import com.situ.mvc.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//  /student/insert   /student/deleteById
@Controller
// 请求映射，浏览器过来的请求：/student
@RequestMapping("/student")
public class StudentController {
    //   /student/insert.action
    @RequestMapping("/insert1")
    public void insert1(String name, Integer age, String gender) {
        System.out.println("StudentController.insert1");
        System.out.println(name);
        System.out.println(age);
        System.out.println(gender);
    }

    //   /student/insert.action
    @RequestMapping("/insert2")
    public ModelAndView insert2(Student student) {
        System.out.println("StudentController.insert2");
        System.out.println(student);

        // Model：模型 数据 View：界面
        ModelAndView modelAndView = new ModelAndView();
        // request.setAttribute("student", student);
        modelAndView.addObject("student", student);
        // request.getRequestDispatcher("/student_info.jsp").forward();
        modelAndView.setViewName("/jsp/student_info.jsp");
        return modelAndView;
    }

    // 默认情况下既支持Get，又支持Post请求
    // 如果只希望这个方法只能处理Post请求
    //@RequestMapping("/insert")
    @RequestMapping(value = "/insert3", method = RequestMethod.POST)
    public String insert3(Student student, Model model) {
        System.out.println("StudentController.insert");
        System.out.println(student);

        // Model：模型 数据 View：界面
        // request.setAttribute("student", student);
        model.addAttribute("student", student);
        // request.getRequestDispatcher("/student_info.jsp").forward();
        // return "/jsp/student_info.jsp";
        return "student_info";
    }

    @RequestMapping("/insert")
    public void insert(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {
        System.out.println("StudentController.insert**");
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String gender = request.getParameter("gender");
        Student student = new Student(name, Integer.parseInt(age), gender);
        System.out.println(student);

        request.setAttribute("student", student);
        request.getRequestDispatcher("/jsp/student_info.jsp").forward(request, response);
    }

    // http://localhost:8080/SpringMVC/student/deleteAll.action?ids=2&ids=3&ids=4
    @RequestMapping("/deleteAll")
    public void deleteAll(Integer[] ids, HttpServletRequest request) {
        System.out.println("StudentController.deleteAll");
        String[]idArray = request.getParameterValues("ids");
        System.out.println("idArray: " + Arrays.toString(idArray));
        System.out.println("ids: " + Arrays.toString(ids));

        // 转发、重定向、返回JSON
    }

    // /SpringMVC/student/deleteById.action?id=23
    @RequestMapping("/deleteById")
    public String deleteById(Integer id) {
        System.out.println("StudentController.deleteById");
        System.out.println("id: " + id);

        // response.sendRedirect("/SpringMVC/student/selectAll.action");
        return "redirect:/student/selectAll.action";
    }

    // 参数作为路径的一部分 （了解）
    // /SpringMVC/student/deleteById1/12.action
    @RequestMapping("/deleteById1/{id}")
    public String deleteById1(@PathVariable("id") Integer id) {
        System.out.println("StudentController.deleteById1");
        System.out.println("id: " + id);

        // response.sendRedirect("/SpringMVC/student/selectAll.action");
        return "redirect:/student/selectAll.action";
    }

    @RequestMapping("/selectAll")
    public String selectAll() {
        System.out.println("StudentController.selectAll");
        // return "/jsp/student_info.jsp";
        return "student_info";
    }

    @RequestMapping("/selectById")
    @ResponseBody
    public Student selectById(Integer id) {
        System.out.println("StudentController.selectById");
        Student student = new Student(1, "zhangsan", 3, "男");
        // {}  [{},{}]
        return student;
    }

    @RequestMapping("/select")
    @ResponseBody
    public List<Student> select() {
        System.out.println("StudentController.select");
        Student student1 = new Student(1, "zhangsan1", 3, "男");
        Student student2 = new Student(2, "zhangsan2", 3, "男");
        Student student3 = new Student(3, "zhangsan3", 3, "男");
        Student student4 = new Student(4, "zhangsan4", 3, "男");
        Student student5 = new Student(5, "zhangsan5", 3, "男");
        // {}  [{},{}]
        List<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        list.add(student5);
        return list;
    }

    // /SpringMVC/student/selectByPage.action?pageNo=3&pageSize=34
    @RequestMapping("/selectByPage")
    @ResponseBody
    public List<Student> selectByPage(@RequestParam(/*value = "page"*/ required = true) Integer pageNo,@RequestParam(defaultValue = "10") Integer pageSize) {
        System.out.println("StudentController.selectByPage");
        System.out.println("pageNo: " + pageNo);
        System.out.println("pageSize: " + pageSize);

        Student student1 = new Student(1, "zhangsan1", 3, "男");
        Student student2 = new Student(2, "zhangsan2", 3, "男");
        Student student3 = new Student(3, "zhangsan3", 3, "男");
        Student student4 = new Student(4, "zhangsan4", 3, "男");
        Student student5 = new Student(5, "zhangsan5", 3, "男");
        // {}  [{},{}]
        List<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        list.add(student5);
        return list;
    }

}
